﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;
using TCPClient.UI.UserControls;

namespace TCPClient.UI
{
    public partial class SendWorkerRequestFroManagerForm : Form
    {

        private ApplWorker m_WorkerRef;

        public SendWorkerRequestFroManagerForm(ApplWorker workerRef)
        {
            m_WorkerRef = workerRef;
            InitializeComponent();
            initTheWeekControlAndDays();
            drawShiftsAccordingToTheViewState();
            this.Text += @" - " + m_WorkerRef.PersonalDetails.FullName;
        }

        

        private void drawShiftsAccordingToTheViewState()
        {
            drawShiftsThatWorkersRequested();
        }

        private void initTheWeekControlAndDays()
        {
            weekTableUserControl1.SetWeekData(ScMainUserControlLogicManager.Instance.StartTimeOfCurrentWeek.AddDays(7), ScMainUserControlLogicManager.Instance.EndTimeOfCurrentWeek.AddDays(7));
            weekTableUserControl1.SetTheGreenRedControlToEachShiftCell(redGreenSelectionUserControl1);

        }

        private void drawShiftsThatWorkersRequested(bool cleanBoard = true)
        {
            if (cleanBoard)
            {
                weekTableUserControl1.CleanWeekBoard();
            }
            foreach (var shift in ScMainUserControlLogicManager.Instance.GetAllWantedShifts(m_WorkerRef.PersonalDetails.Id))
            {
                weekTableUserControl1.DrawShiftsThatWorkersRequested(shift.BeginTime, shift.EndTime, eType.WANTED);
            }
            foreach (var shift in ScMainUserControlLogicManager.Instance.GetAllForbiddenShifts(m_WorkerRef.PersonalDetails.Id))
            {
                weekTableUserControl1.DrawShiftsThatWorkersRequested(shift.BeginTime, shift.EndTime, eType.FORBIDDEN);
            }
        }

        private void buttonSendRequests_Click(object sender, EventArgs e)
        {
            List<ShiftRequestFromGUI> requests = new List<ShiftRequestFromGUI>();
            foreach (var dayInWeek in weekTableUserControl1.Days)
            {
                foreach (var shiftInDay in dayInWeek.ShiftsInDay)
                {
                    if (shiftInDay.CurrentState == eCurrentState.Green || shiftInDay.CurrentState == eCurrentState.Red)
                    {
                        requests.Add(new ShiftRequestFromGUI()
                        {
                            EndDate = shiftInDay.EndTime,
                            StartDate = shiftInDay.StartTime,
                            Type = shiftInDay.CurrentState == eCurrentState.Green ? eType.WANTED : eType.FORBIDDEN,
                        });
                    }
                }
            }
            ScMainUserControlLogicManager.Instance.UpdateServerWithWorkerRequest(requests, m_WorkerRef);
            MessageBox.Show(@"Your request was successfully updated", @"Update Successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
